{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public class GoodNumberCounter {\n",
    "\n",
    "    public static void main(String[] args) {\n",
    "        int n = 20; // 可以根据需要更改 n 的值\n",
    "        int result = countGoodNumbers(n);\n",
    "        System.out.println(result);\n",
    "    }\n",
    "\n",
    "    public static int countGoodNumbers(int n) {\n",
    "        int count = 0;\n",
    "        for (int i = 1; i <= n; i++) {\n",
    "            if (isGoodNumber(i)) {\n",
    "                count++;\n",
    "            }\n",
    "        }\n",
    "        return count;\n",
    "    }\n",
    "\n",
    "    private static boolean isGoodNumber(int num) {\n",
    "        String numStr = String.valueOf(num);\n",
    "        for (int i = 1; i < numStr.length(); i++) {\n",
    "            int prevDigit = numStr.charAt(i - 1) - '0';\n",
    "            int currDigit = numStr.charAt(i) - '0';\n",
    "            if (Math.abs(currDigit - prevDigit) != 1) {\n",
    "                return false;\n",
    "            }\n",
    "        }\n",
    "        return true;\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.*;\n",
    "\n",
    "public class Solution {\n",
    "    /**\n",
    "     * 统计满足条件的区间数目\n",
    "     *\n",
    "     * @param a int整型一维数组，输入的数组\n",
    "     * @param x int整型，给定的目标值 x\n",
    "     * @return int整型，满足条件的区间个数\n",
    "     */\n",
    "    public int countInterval(int[] a, int x) {\n",
    "        int count = 0;\n",
    "        int n = a.length;\n",
    "\n",
    "        // 遍历所有可能的区间 [L, R]\n",
    "        for (int L = 0; L < n; L++) {\n",
    "            int xorSum = 0;\n",
    "\n",
    "            for (int R = L; R < n; R++) {\n",
    "                // 计算 [L, R] 区间的异或和\n",
    "                xorSum ^= a[R];\n",
    "\n",
    "                // 检查是否满足条件\n",
    "                if (xorSum == x && (R - L + 1) % 2 == 0) {\n",
    "                    count++;\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        return count;\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.*;\n",
    "\n",
    "public class Solution {\n",
    "    /**\n",
    "     * 统计满足条件的区间数目\n",
    "     *\n",
    "     * @param a int整型一维数组，输入的数组\n",
    "     * @param x int整型，给定的目标值 x\n",
    "     * @return int整型，满足条件的区间个数\n",
    "     */\n",
    "    public int countInterval(int[] a, int x) {\n",
    "        int count = 0;\n",
    "        int n = a.length;\n",
    "        int prefixXor = 0;\n",
    "        \n",
    "        // 使用 HashMap 存储前缀异或和及其出现的次数\n",
    "        Map<Integer, Integer> prefixXorMap = new HashMap<>();\n",
    "        prefixXorMap.put(0, 1); // 初始化前缀异或和为 0 的情况\n",
    "        \n",
    "        for (int i = 0; i < n; i++) {\n",
    "            // 更新当前前缀异或和\n",
    "            prefixXor ^= a[i];\n",
    "            \n",
    "            // 计算所需的前缀异或和\n",
    "            int target = prefixXor ^ x;\n",
    "            \n",
    "            // 检查 target 是否在 HashMap 中，且区间长度为偶数\n",
    "            if (prefixXorMap.containsKey(target) && i % 2 == 1) {\n",
    "                count += prefixXorMap.get(target);\n",
    "            }\n",
    "            \n",
    "            // 更新 HashMap\n",
    "            prefixXorMap.put(prefixXor, prefixXorMap.getOrDefault(prefixXor, 0) + 1);\n",
    "        }\n",
    "        \n",
    "        return count;\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.HashMap;\n",
    "import java.util.Map;\n",
    "\n",
    "public class Solution {\n",
    "\n",
    "    public int countInterval(int[] a, int x) {\n",
    "        int count = 0;\n",
    "        int prefixXor = 0;\n",
    "        \n",
    "        // 使用 HashMap 存储前缀异或和以及索引的奇偶性\n",
    "        Map<Integer, int[]> prefixXorMap = new HashMap<>();\n",
    "        prefixXorMap.put(0, new int[]{1, 0}); // 初始化前缀异或和为0的情况：偶数索引1次，奇数索引0次\n",
    "        \n",
    "        for (int i = 0; i < a.length; i++) {\n",
    "            // 更新当前前缀异或和\n",
    "            prefixXor ^= a[i];\n",
    "            \n",
    "            // 计算目标前缀异或和\n",
    "            int target = prefixXor ^ x;\n",
    "            \n",
    "            // 检查 HashMap 是否包含目标前缀异或和\n",
    "            if (prefixXorMap.containsKey(target)) {\n",
    "                if (i % 2 == 0) {\n",
    "                    count += prefixXorMap.get(target)[0]; // 偶数索引情况\n",
    "                } else {\n",
    "                    count += prefixXorMap.get(target)[1]; // 奇数索引情况\n",
    "                }\n",
    "            }\n",
    "            \n",
    "            // 更新 HashMap，记录当前前缀异或和的奇偶索引出现次数\n",
    "            prefixXorMap.putIfAbsent(prefixXor, new int[2]);\n",
    "            if (i % 2 == 0) {\n",
    "                prefixXorMap.get(prefixXor)[0]++;\n",
    "            } else {\n",
    "                prefixXorMap.get(prefixXor)[1]++;\n",
    "            }\n",
    "        }\n",
    "        \n",
    "        return count;\n",
    "    }\n",
    "}\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
